<?php
/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
require_once 'Savant2/Plugin.php';

class Savant2_Plugin_leagueman_show_leaguetable2 extends Savant2_Plugin
{

    /**
     * function plugin
     * @param object $project LM_Table_Project
     * @param object $round LM_Table_Round
     * @param object $tc Tableconfig
     * @param int $mode 0 = all, 1 = home, 2 = away
     * @return string
     */
    function plugin(LM_Table_Project $project, Tableconfig $tc)
    {
        global $mainframe, $Itemid;

        $matchtable = LM::getLMTable("Match");
        $matchtable->addFilter("m.match_project = " . $project->getID());
        $matchtable->addFilter("m.match_round BETWEEN " . $tc->getVar("limit_from") . " AND " . $tc->getVar("limit_to"));
        if ($tc->getVar("group", "") != "") {
            $matchtable->addFilter("m.match_group = '" . $tc->getVar("group") . "'");
        }
        $matchtable->addFilter("m.published = 1");

        $myItemid = $tc->getVar("itemid", $Itemid);

        require_once "Leaguetable.php";

        $leaguetable = new Leaguetable_Football($project);
        $leaguetable->setResults($matchtable->getResults());
        $a = $leaguetable->getTable($tc->getVar("mode"));

        $colors = $project->getColors();

        $min = 1;
        $max = count($a);

        $delta = $tc->getVar("delta");

        if ($delta > 0 && $team = $tc->getVar("fav_team")) {
            $i = 1;
            foreach ($a AS $key => $val) {
                if ($key == $team) {
                    break;
                } else {
                    $i++;
                }
            }

            $min = $this->getMinimum(count($a), $i, $delta);
            $max = $this->getMaximum(count($a), $i, $delta);
        }

        ob_start();
?>
        <table width="100%" cellspacing="0">
            <tr>
                <td class="sectiontableheader" align="center"></td>
                <td class="sectiontableheader" align="left"><?php echo _LM_TEAM; ?></td>
                
                <?php if ($tc->show_cnt_matches) { ?>
                <td class="sectiontableheader" align="center"><?php echo _LM_MATCHES; ?></td>
                <?php } ?>
                
                <?php if ($tc->show_cnt_v) { ?>
                <td class="sectiontableheader" align="center"><?php echo _LM_W; ?></td>
                <?php } ?>
                
                <?php if ($tc->show_cnt_v1) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_W1; ?></td>
<?php
        }
        if ($tc->show_cnt_v2) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_W2; ?></td>
<?php
        }
        if ($tc->show_cnt_t) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_D; ?></td>
<?php
        }
        if ($tc->show_cnt_d) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_L; ?></td>
<?php
        }
        if ($tc->show_cnt_d1) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_L1; ?></td>
<?php
        }
        if ($tc->show_cnt_d2) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_L2; ?></td>
<?php
        }
        if ($tc->show_results) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_GOALS; ?></td>
<?php
        }
        if ($tc->show_diff) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_DIFF; ?></td>
<?php
        }
        if ($tc->show_points) {
?>
            <td class="sectiontableheader" align="center"><?php echo _LM_POINTS; ?></td>
<?php
        }
?>
    </tr>

<?php
        $counter = 1;
        $k = 0;
        $j = 0;
        foreach ($a as $akey => $aval) {

            if ($min <= $counter && $max >= $counter) {

                $class = ($k == 0) ? 'sectiontableentry1' : 'sectiontableentry2';

                $color = "";
                if (count($colors)) {
                    if ($counter == @$colors[$j]["from"])
                        $color = @$colors[$j]["color"];
                    if ($counter > @$colors[$j]["from"] && $counter <= @$colors[$j]["to"])
                        $color = @$colors[$j]["color"];
                    if ($counter == @$colors[$j]["to"])
                        $j++;
                }

                $format = "%s";
                if ($aval->getTeam()->team_id == $tc->getVar("fav_team")) {
                    $format = "<b>%s</b>";
                }

                $team_name = $tc->getVar("shortnames", 0) ? $aval->getTeam()->team_name_short : $aval->getTeam()->team_name;

                if ($tc->getVar("link_teams")) {
                    $link = "<a href=\"" . sefRelToAbs("index.php?option=com_leagueman&amp;func=teaminfo&amp;tm=" . $aval->getTeam()->teammap_id . "&amp;Itemid=" . $myItemid) . "\">";
                    $link .= sprintf($format, $team_name);
                    $link .= "</a>";
                } else {
                    $link = sprintf($format, $team_name);
                }
?>
                <tr class="<?php echo $class; ?>">
                    <td bgcolor="<?php echo $color; ?>"><div align="right"><?php printf($format, $counter); ?></div></td>
                <td bgcolor="<?php echo $color; ?>"><div align="left"><?php echo $link; ?></div></td>
        <?php
                if ($tc->getVar("show_cnt_matches")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getMatches()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_v")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getV1()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_v1")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getV2()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_v2")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getV3()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_t")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getT1()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_d")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getD1()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_d1")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getD2()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_cnt_d2")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getD3()); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_results")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, sprintf("%s : %s", $aval->getScore1(), $aval->getScore2())); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_diff")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, sprintf("%+d", $aval->getDiff())); ?></div></td>
        <?php
                }
                if ($tc->getVar("show_points")) {
        ?>
                    <td bgcolor="<?php echo $color; ?>"><div align="center"><?php printf($format, $aval->getPoints()); ?></div></td>
        <?php
                }
        ?>
                </tr>
    <?php
            }

            $k = 1 - $k;
            $counter++;
        }
    ?>
    </table>

<?php
        $html = ob_get_contents();
        ob_end_clean();

        return $html;
    }

    function getMinimum($total, $position, $delta)
    {

        if ($position <= $delta) {
            $x = 1;
        } else if ($position >= $total - $delta) {
            $x = $total - $delta * 2;
        } else {
            $x = $position - $delta;
        }

        return $x;
    }

    function getMaximum($total, $position, $delta)
    {

        if ($position <= $delta) {
            $x = $delta * 2 + 1;
        } else if ($position >= $total - $delta) {
            $x = $total;
        } else {
            $x = $position + $delta;
        }

        return $x;
    }

}